package com.my.javaweb;

import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/******
 *
 *
 *
 * */
@WebServlet(
        name = "RequestDemo",
        urlPatterns = {"/req"}
)
public class RequestDemo extends HttpServlet {

    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        //获取请求头
            String method = req.getMethod();
            System.out.println("获取请求方式:" + method);
            StringBuffer url = req.getRequestURL();
            System.out.println("获取请求URL：" + url);
            String h = req.getScheme();
            System.out.println("获取请求协议：" + h);
            String key = req.getHeader("");
            System.out.println("获取指定键名的键值：" + key);
            Enumeration keys = req.getHeaderNames();
        //获取请求行
            String name;
            String pwd;
            while(keys.hasMoreElements()) {
                name = (String)keys.nextElement();
                pwd = req.getHeader(name);
                System.out.println(name + ":" + pwd);
            }
        //获取请求信息
            name = req.getParameter("uname");
            pwd = req.getParameter("pwd");
            System.out.println("用户数据：" + name + ":" + pwd);
            String[] hobbies = req.getParameterValues("hobbies");
            if (hobbies != null) {
                for(String hobby:hobbies){
                    System.out.println("同hobbies键不同值："+hobby);
                }
            } else {
                System.out.println("没有爱好");
            }

    }
}
